# BBS 即时通讯系统 Makefile

# 变量定义
APP_NAME = bbs-web
NODE_BIN = node
NPM_BIN = npm
PORT = 3000
INSTALL_DIR = /opt/$(APP_NAME)

# 默认目标
all: install start

# 安装依赖
install:
	@echo "安装项目依赖..."
	$(NPM_BIN) install

# 开发模式运行
dev:
	@echo "以开发模式启动应用..."
	$(NPM_BIN) start

# 启动应用
start:
	@echo "启动应用..."
	$(NODE_BIN) server/index.js

# 停止应用
stop:
	@echo "停止应用..."
	@-pkill -f "node server/index.js" || true

# 系统安装（需要root权限）
system-install: install
	@echo "系统级安装应用到 $(INSTALL_DIR)..."
	mkdir -p $(INSTALL_DIR)
	cp -r . $(INSTALL_DIR)
	chmod +x $(INSTALL_DIR)/server/index.js
	
	# 创建系统服务文件
	@echo "创建系统服务..."
	@echo "[Unit]" > /tmp/$(APP_NAME).service
	@echo "Description=BBS Web Service" >> /tmp/$(APP_NAME).service
	@echo "After=network.target" >> /tmp/$(APP_NAME).service
	@echo "" >> /tmp/$(APP_NAME).service
	@echo "[Service]" >> /tmp/$(APP_NAME).service
	@echo "Type=simple" >> /tmp/$(APP_NAME).service
	@echo "User=nobody" >> /tmp/$(APP_NAME).service
	@echo "WorkingDirectory=$(INSTALL_DIR)" >> /tmp/$(APP_NAME).service
	@echo "ExecStart=$(NODE_BIN) server/index.js" >> /tmp/$(APP_NAME).service
	@echo "Restart=on-failure" >> /tmp/$(APP_NAME).service
	@echo "" >> /tmp/$(APP_NAME).service
	@echo "[Install]" >> /tmp/$(APP_NAME).service
	@echo "WantedBy=multi-user.target" >> /tmp/$(APP_NAME).service
	
	mv /tmp/$(APP_NAME).service /etc/systemd/system/
	systemctl daemon-reload
	systemctl enable $(APP_NAME).service
	systemctl start $(APP_NAME).service
	@echo "系统服务已安装并启动"

# 系统卸载（需要root权限）
system-uninstall:
	@echo "卸载系统服务..."
	-systemctl stop $(APP_NAME).service
	-systemctl disable $(APP_NAME).service
	-rm /etc/systemd/system/$(APP_NAME).service
	systemctl daemon-reload
	@echo "删除安装目录 $(INSTALL_DIR)..."
	-rm -rf $(INSTALL_DIR)
	@echo "卸载完成"

# 清理项目
clean:
	@echo "清理项目..."
	rm -rf node_modules
	rm -rf data
	rm -rf uploads/*
	@echo "清理完成"

# 设置开发环境
setup-dev:
	@echo "设置开发环境..."
	mkdir -p data uploads
	chmod -R 755 uploads
	@echo "添加启动脚本到 package.json..."
	$(NPM_BIN) pkg set scripts.start="node server/index.js"
	$(NPM_BIN) pkg set scripts.dev="nodemon server/index.js"
	@echo "开发环境设置完成"
	@echo "可以通过 'npm run dev' 启动开发服务器"

# 构建并运行Docker容器
docker-build:
	@echo "构建Docker镜像..."
	docker build -t $(APP_NAME) .

docker-run:
	@echo "运行Docker容器..."
	docker run -d -p $(PORT):3000 --name $(APP_NAME)-container $(APP_NAME)

docker-stop:
	@echo "停止Docker容器..."
	docker stop $(APP_NAME)-container
	docker rm $(APP_NAME)-container

# 帮助信息
help:
	@echo "BBS 即时通讯系统 Makefile 帮助"
	@echo "---------------------------"
	@echo "可用命令:"
	@echo "  make all            - 安装依赖并启动应用"
	@echo "  make install        - 安装项目依赖"
	@echo "  make dev            - 以开发模式启动应用"
	@echo "  make start          - 启动应用"
	@echo "  make stop           - 停止应用"
	@echo "  make clean          - 清理项目"
	@echo "  make setup-dev      - 设置开发环境"
	@echo "  make system-install - 系统级安装应用（需要root权限）"
	@echo "  make system-uninstall - 卸载系统服务（需要root权限）"
	@echo "  make docker-build   - 构建Docker镜像"
	@echo "  make docker-run     - 运行Docker容器"
	@echo "  make docker-stop    - 停止Docker容器"
	@echo "  make help           - 显示此帮助信息"

.PHONY: all install dev start stop clean setup-dev system-install system-uninstall docker-build docker-run docker-stop help 